using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class AlbumSlideCommentView : BasePage
	{
		private enum InputMode
		{
			InputTxtMode,
			InputSmileyMode,
			NoneMode
		}
		private DelegateComment mCommentCallBack;
		private bool bIsFirstLoad = true;
		private AlbumSlideCommentView.InputMode nowMode = AlbumSlideCommentView.InputMode.NoneMode;
		private ApplicationBar mBar;
		private ApplicationBarIconButton OkMenuBtn;
		private ApplicationBarIconButton smelyBtn;
		internal Grid LayoutRoot;
		internal ChatInputTextBox InputTextBox;
		internal SmileyOverlay SmileyView;
		private bool _contentLoaded;
		public AlbumSlideCommentView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.AlbumSlideCommentView_Loaded));
		}
		private void AlbumSlideCommentView_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.bIsFirstLoad)
			{
				this.InitResource();
				this.InitSmiley();
				base.set_ApplicationBar(this.GetApplicationBar());
				this.InputTextBox.add_GotFocus(new RoutedEventHandler(this.InputTextBox_GotFocus));
				this.bIsFirstLoad = false;
			}
			if (this.InputTextBox != null && this.InputTextBox.realBox != null)
			{
				this.InputTextBox.FocusRealBox();
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && object_0.Length != 0 && object_0[0] != null && object_0[0] is DelegateComment)
			{
				this.mCommentCallBack = (object_0[0] as DelegateComment);
			}
		}
		protected override void OnPageOut()
		{
		}
		private void InitResource()
		{
			this.LayoutRoot.set_Background(R.theme.SNS_MAINPAGE_BK_BRUSH);
		}
		private void SwitchToMode(AlbumSlideCommentView.InputMode mode)
		{
			if (mode == AlbumSlideCommentView.InputMode.InputSmileyMode)
			{
				base.Focus();
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.SmileyView.set_Visibility(0);
				});
				this.nowMode = AlbumSlideCommentView.InputMode.InputSmileyMode;
			}
			else
			{
				if (mode == AlbumSlideCommentView.InputMode.InputTxtMode)
				{
					this.SmileyView.set_Visibility(1);
					this.nowMode = AlbumSlideCommentView.InputMode.InputTxtMode;
				}
				else
				{
					if (mode == AlbumSlideCommentView.InputMode.NoneMode)
					{
						this.SmileyView.set_Visibility(1);
						base.Focus();
						this.nowMode = AlbumSlideCommentView.InputMode.NoneMode;
					}
				}
			}
			this.RefreshAppBar();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (this.nowMode == AlbumSlideCommentView.InputMode.InputSmileyMode)
			{
				this.SwitchToMode(AlbumSlideCommentView.InputMode.NoneMode);
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		private void InitSmiley()
		{
			this.SmileyView.Click = new SmileyClick(this.SmileyClick);
			this.SmileyView.EmojiClick = new SmileyClick(this.EmojiClick);
		}
		private void SmileyClick(int smiley)
		{
			if (smiley == -1)
			{
				this.SwitchToMode(AlbumSlideCommentView.InputMode.NoneMode);
				return;
			}
			if (smiley != -2)
			{
				int selectionStart = this.InputTextBox.realBox.get_SelectionStart();
				string text = this.InputTextBox.Text;
				string text2 = text.Substring(0, selectionStart);
				string smileyText = SmileyManager.GetInstance().getSmileyText(smiley);
				this.SmileyView.PushSmileyStack(smileyText);
				string text3 = text2 + smileyText;
				int length = text3.get_Length();
				text3 += text.Substring(selectionStart);
				this.InputTextBox.Text = text3;
				this.InputTextBox.realBox.set_SelectionStart(length);
				this.InputTextBox.realBox.set_SelectionLength(0);
				return;
			}
			if (this.InputTextBox.realBox.get_SelectionLength() > 0)
			{
				int selectionStart2 = this.InputTextBox.realBox.get_SelectionStart();
				int selectionLength = this.InputTextBox.realBox.get_SelectionLength();
				string text4 = this.InputTextBox.Text;
				string text5 = text4.Substring(0, selectionStart2) + text4.Substring(selectionStart2 + selectionLength);
				this.InputTextBox.Text = text5;
				this.InputTextBox.realBox.set_SelectionStart(selectionStart2);
				this.InputTextBox.realBox.set_SelectionLength(0);
				return;
			}
			int selectionStart3 = this.InputTextBox.realBox.get_SelectionStart();
			string text6 = this.InputTextBox.Text;
			string text7 = text6.Substring(0, selectionStart3);
			string text8 = this.SmileyView.RemoveLastSmiley(text7);
			int num = text7.get_Length() - text8.get_Length();
			text7 = text8 + text6.Substring(selectionStart3);
			this.InputTextBox.Text = text7;
			this.InputTextBox.realBox.set_SelectionStart(selectionStart3 - num);
			this.InputTextBox.realBox.set_SelectionLength(0);
		}
		private void EmojiClick(int emojiIndex)
		{
			int selectionStart = this.InputTextBox.realBox.get_SelectionStart();
			string text = this.InputTextBox.Text;
			string text2 = text.Substring(0, selectionStart);
			string utf16CodeByIndex = EmojiManager.GetInstance().GetUtf16CodeByIndex(emojiIndex);
			this.SmileyView.PushSmileyStack(utf16CodeByIndex);
			string text3 = text2 + utf16CodeByIndex;
			int length = text3.get_Length();
			text3 += text.Substring(selectionStart);
			this.InputTextBox.Text = text3;
			this.InputTextBox.realBox.set_SelectionStart(length);
			this.InputTextBox.realBox.set_SelectionLength(0);
		}
		private void InputTextBox_GotFocus(object sender, RoutedEventArgs e)
		{
			this.SwitchToMode(AlbumSlideCommentView.InputMode.InputTxtMode);
		}
		private void RefreshAppBar()
		{
			if (base.get_ApplicationBar() == null)
			{
				return;
			}
			if (this.nowMode == AlbumSlideCommentView.InputMode.NoneMode)
			{
				if (base.get_ApplicationBar().get_Buttons().get_Count() > 1)
				{
					base.get_ApplicationBar().get_Buttons().RemoveAt(0);
					return;
				}
			}
			else
			{
				if (base.get_ApplicationBar().get_Buttons().get_Count() == 1)
				{
					base.get_ApplicationBar().get_Buttons().Insert(0, this.smelyBtn);
				}
				if (this.nowMode == AlbumSlideCommentView.InputMode.InputSmileyMode)
				{
					this.smelyBtn.set_Text(strings.Album_TakingPhoto_Text);
					this.smelyBtn.set_IconUri(new Uri("/Resource/Img/Chat/keyboard.png", 2));
					return;
				}
				this.smelyBtn.set_Text(strings.Album_TakingPhoto_Smely);
				this.smelyBtn.set_IconUri(new Uri("/Resource/Img/Chat/expression.png", 2));
			}
		}
		private ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			this.OkMenuBtn = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			this.OkMenuBtn.set_Text(strings.COM_Confirm);
			this.OkMenuBtn.add_Click(new EventHandler(this.OkMenuBtn_Click));
			this.mBar.get_Buttons().Add(this.OkMenuBtn);
			this.smelyBtn = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			this.smelyBtn.set_Text(strings.Album_TakingPhoto_Smely);
			this.smelyBtn.add_Click(new EventHandler(this.smelyBtn_Click));
			return this.mBar;
		}
		private void smelyBtn_Click(object sender, EventArgs e)
		{
			if (this.nowMode != AlbumSlideCommentView.InputMode.NoneMode && this.nowMode != AlbumSlideCommentView.InputMode.InputTxtMode)
			{
				if (this.InputTextBox != null && this.InputTextBox.realBox != null)
				{
					this.InputTextBox.FocusRealBox();
				}
				return;
			}
			this.SwitchToMode(AlbumSlideCommentView.InputMode.InputSmileyMode);
		}
		private void OkMenuBtn_Click(object sender, EventArgs e)
		{
			if (this.mCommentCallBack != null)
			{
				string text = this.InputTextBox.Text.Trim();
				if (!string.IsNullOrEmpty(text))
				{
					bool flag = false;
					bool flag2 = false;
					string text2 = EmojiManager.GetInstance().ConvertInputStrToCodeStr(text, ref flag);
					text2 = EmojiManager.GetInstance().SbCodeToUtf16Code(text2, ref flag2);
					this.InputTextBox.Text = "";
					this.SmileyView.ClearSmileyStack();
					this.mCommentCallBack(text2);
					base.GoBack();
					return;
				}
				this.SwitchToMode(AlbumSlideCommentView.InputMode.InputTxtMode);
				MessageBoxManager.ShowToast(strings.ChatEmptyTxtHint, 2500, null, false);
			}
		}
		private void Grid_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			this.SwitchToMode(AlbumSlideCommentView.InputMode.NoneMode);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Album/UI/AlbumSlideCommentView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.InputTextBox = (ChatInputTextBox)base.FindName("InputTextBox");
			this.SmileyView = (SmileyOverlay)base.FindName("SmileyView");
		}
	}
}
